package hottargui.config;


import hottargui.framework.*;
import java.util.Map;
import hottargui.standard.*;


public class AlphaEndRoundState implements TarguiState {

	Game gameContext;
	
	public AlphaEndRoundState(Game game)
	{
		this.gameContext = game;
	}
	
	@Override
	public boolean buy(int count, Position deploy) throws IllegalActionException {
		throw new IllegalActionException(this.getState());
	}

	@Override
	public State getState() {
		return State.endRound;
	}

	@Override
	public PlayerColor getWinner() throws IllegalActionException {
		throw new IllegalActionException(this.getState());
	}

	@Override
	public boolean move(Position from, Position to, int count) throws IllegalActionException {
		throw new IllegalActionException(this.getState());
	}

	@Override
	public void newGame() throws IllegalActionException {
		throw new IllegalActionException(this.getState());
	}

	@Override
	public void rollDie() throws IllegalActionException {
		throw new IllegalActionException(this.getState());
	}

	@Override
	public PlayerColor turnCard() throws IllegalActionException {
		// TODO: Replace this stub
		GameData gd = gameContext.getGameObjectServer().getGameData();
		PlayerColor retval = PlayerColor.None;
		Map<PlayerColor,Player> players =  gd.getPlayers();
		
		for (Tile tile : gd.getBoardConfiguration())
		{
			if (players.containsKey(tile.getOwnerColor())) 
			{
				//Get the player who owns this tile
				StandardPlayer owner = (StandardPlayer)players.get(tile.getOwnerColor());
				
				//Check the owner has a settlement. If not he cannot get any revenue
				if(gameContext.getTile(owner.getSettlement()).getOwnerColor() == owner.getColor())				
					owner.add(tile.getType().getEconomicValue());
			}
		}
		
		if(gd.getRoundsLeft() > 0)
		{
			gd.endRound();
			gameContext.setState(gameContext.getFactory().getMoveState(gameContext));
			retval = gameContext.getGameObjectServer().getTurnSequenceStrategy().getNextPlayerColor();
		}
		else
		{
			gameContext.setState(gameContext.getFactory().getEndGameState(gameContext));
			retval =PlayerColor.None;
		}

		return retval;
	}
	
	
}
